$(function(){
	var flag=true;
	common.goBackNext();
	var houseId =common.getQueryString('houseId');
	var subVisitCount =common.getQueryString('subVisitCount');
	var subVisitPoint =common.getQueryString('subVisitPoint');
	var token=common.getToken();
	var agentInfo=common.getLocalData('H5-agentInfo');
	var sendData={
        pageNum:1,
        numPerPage:20,
        token:token,
        houseId:houseId
    };
    var documentHeight=0;
    var windowHeight=0;
    var $resMsg=$("#res-msg");
    var $loadingFail=$('.loading-fail');
	var globalHost=common.globalHost;
	var getHouseVisitListByHouseId=common.getHouseVisitListByHouseId;
	var url=globalHost+getHouseVisitListByHouseId;
	var $visitedList=$('#visited-list');
	var $subVisitCount=$('#subVisitCount');
	var $subVisitPoint=$('#subVisitPoint');
	// var $closeIt=$('#close-it');
	// $closeIt.on('change',function(e){
	// 	var $this=$(this);
	// 	$visitedList.html('');
	// 	sendData={
	// 		pageNum:1,
	//         numPerPage:20,
	//         year:$this.val(),
	//         token:token,
	//         houseId:houseId
	// 	}
	// 	requestData(sendData);
	// 	return false;
	// 	e.preventDefault();
	// });
	requestData(sendData);
	$subVisitPoint.html(subVisitPoint);
	$subVisitCount.html(subVisitCount);
	$(window).on("scroll",function (e) {
        windowHeight=$(window).height();
        if($(this).scrollTop()+windowHeight==documentHeight){
            if(flag){
                requestData(sendData);
            };
        }
    });
    function buildGetMoneyList(options){
	    var dataList=options.dataList||[];
	    var $loading=$(options.loadingSelector);
	    var arrTemp=[];
	    var dataCount=0;
	    dataList.length<=sendData.numPerPage?$loading.text("已经没有更多信息了").show():"";
	    $.each(dataList,function(idx,obj){
	        dataCount++;
	       	var createTime=obj['createTime']?obj['createTime']:"";
	       	createTime =common.getYearMonthAndDay(createTime);
	       	var point=obj['point']?obj['point']:'';
	       	var mobile=obj['mobile']?obj['mobile']:"";
	       	var verifyStatus=obj['verifyStatus']?obj['verifyStatus']:"";//0处理中；1已到账；2失败
	       	if(verifyStatus==1){
	       		verifyStatus='<p>+'+point+'积分</p>';
	       	}else if(verifyStatus==0){
	       		verifyStatus='<p class="dealing">处理中</p>';
	       	}else if(verifyStatus==2){
	       		verifyStatus='<p>+0积分</p>';
	       	}  	
	       	var agentName=obj['agentName']?obj['agentName']:'';
	        var tempDom='<li>'+
	                       	'<div class="clearfix">'+
					        	'<div class="date">'+createTime+'</div>'+
					        	'<div class="visited-type">'+agentName+'</div>'+
					        	'<div class="telphone">'+mobile+'</div>'+
					        	verifyStatus+
				        	'</div>'+
	                    '</li>';
	        arrTemp.push(tempDom);
	    });
	    return arrTemp.join("");
	};
	function requestData(sendData) {
	    $.ajax({
	        data:sendData,
	        type: "POST",
	        contentType: "application/x-www-form-urlencoded;charset=utf-8",
	        dataType:"json",
	        url:url,
	        crossDomain:true,
	        success:function(data){
	            if(typeof data==="string"){
	                data=JSON.parse(data);
	            }
	            if(data.code==101 ||data.code==102){
	                console.log("token为空或者token失效");
	                window.location.href='login.html';
	            };
	            $resMsg.text("").hide();
	            $loadingFail.hide();
	            if(!!data && data['code']=="0"){
	                var totalNum=data.res.data.page.recordCount;
	                if(totalNum==0){
	                    $loadingFail.show();
	                }else{
	                    data=data.res.data.record;
	                    if(!data.length){
	                        $resMsg.text("已经没有更多信息了").show();
	                        flag=false;
	                    }else{
	                        var responseDom=buildGetMoneyList({
	                            loadingSelector:$resMsg,
	                            dataList:data
	                        });
	                        $visitedList.append(responseDom);
	                        documentHeight=$(document).height();
	                        sendData.pageNum++;
	                        flag=true;
	                    };
	                };
	            };
	        },
	        timeout:10000,
	        error: function (XMLHttpRequest, textStatus, errorThrown) {
	            common.errDialog("网络不畅，请稍后重试");  
	        }
	    });
	};
})